/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.ds;

import java.util.ArrayList;
import java.util.List;

import net.bioteam.appweb.ds.SrcDataset;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.clientandserver.ds.DSProxy;
import net.bioteam.appweb.gwt.apps.clientandserver.ds.DSProxyAsync;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.SortDirection;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class DatasetGrid extends ListGrid
{

	public static final int WIDTH=400;
	
	public DatasetGrid()
	{
		setWidth(WIDTH);
		setHeight100();
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setShowEmptyMessage(true);
		setEmptyMessage("loading...");
		
		setSelectionType(SelectionStyle.SIMPLE);  
		setSelectionAppearance(SelectionAppearance.CHECKBOX);  
		
		setShowHeader(true);

		ListGridField descField = new ListGridField("name", "Name", 160);
		descField.setType(ListGridFieldType.TEXT);
		descField.setSortDirection(SortDirection.ASCENDING);
		
		ListGridField dnaField = new ListGridField("DNA", "DNA", 40);
		dnaField.setType(ListGridFieldType.TEXT);
		dnaField.setCellAlign(Alignment.CENTER);
		
		setFields(descField, dnaField);
	}

	boolean loaded = false;

	public void load()
	{
		if (loaded)
			return;
		loaded = true;
		ApplicationCallBack<SrcDataset[]> callback = new ApplicationCallBack<SrcDataset[]>()
		{

			public void onFailureAfter(Throwable caught)
			{
				DatasetGrid.this.setEmptyMessage(caught.getMessage());
			}

			public void onSuccessAfter(SrcDataset[] dss)
			{

				ListGridRecord[] list = new ListGridRecord[dss.length];
				List<Integer> selectedIDS=new ArrayList<Integer>();
				for (int i=0; i<dss.length; i++)
				{
					ListGridRecord record = new ListGridRecord();
					record.setAttribute("name", dss[i].getName());
					record.setAttribute("DNA", dss[i].isDNA()?"Y":"N");
					if (dss[i].isSubscribed())
					{
						selectedIDS.add(i);
					}
					list[i] = record;
				}
				
				DatasetGrid.this.setData(list);
				int ids[]=new int[selectedIDS.size()];
				for (int i=0; i<selectedIDS.size(); i++)
				{
					ids[i]=selectedIDS.get(i);
				}
				DatasetGrid.this.selectRecords(ids);

			}

			public void execute()
			{

				DSProxyAsync proxy=GWT.create(DSProxy.class);
				proxy.getDatasets(this);
			}

		};
		callback.execute();

	}

	public void reload()
	{
		invalidateCache();
		loaded = false;
		load();
	}
}
